package jok.set;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class SetDemo {

	public static void main(String[] args) {

		Set<String> set = new HashSet<String>();

		set.add("王五");
		set.add("张三");
		set.add("张三");
		set.add("李四");
		set.add(null);
		set.add(null);

		System.out.println(set); // [null, 张三, 李四, 王五]

		System.out.println("----------------------");

		Set<Student> set2 = new TreeSet<Student>();

		set2.add(new Student(2, "李四", 18));
		set2.add(new Student(2, "李四", 16));
		set2.add(new Student(1, "张三", 28));
		set2.add(new Student(3, "王五", 22));

		System.out.println(set2);
		// [Student [id=1, name=张三, age=28], Student [id=2, name=李四, age=16],
		// Student [id=2, name=李四, age=18], Student [id=3, name=王五, age=22]]

		System.out.println("----------------------");

		Integer[] arr = { 2, 11, 3, 14, 1, 9 };
		show(arr);
		System.out.println();

		System.out.println("----------------------");

		Arrays.sort(arr);
		System.out.println(Arrays.toString(arr));// [1, 2, 3, 9, 11, 14]
		System.out.println(Arrays.binarySearch(arr, 14));// 5

		System.out.println("----------------------");

		List<Student> list = new ArrayList<Student>();

		list.add(new Student(2, "李四", 18));
		list.add(new Student(2, "李四", 16));
		list.add(new Student(1, "张三", 28));
		list.add(new Student(3, "王五", 22));

		Collections.sort(list);
		Collections.reverse(list);
		System.out.println(list);
		// [Student [id=3, name=王五, age=22], Student [id=2, name=李四, age=18],
		// Student [id=2, name=李四, age=16], Student [id=1, name=张三, age=28]]
		Collections.shuffle(list);
		System.out.println(list);
		// [Student [id=2, name=李四, age=16], Student [id=1, name=张三, age=28],
		// Student [id=2, name=李四, age=18], Student [id=3, name=王五, age=22]]

		System.out.println("----------------------");

		StudentComparator sc = new StudentComparator();
		Collections.sort(list, sc);
		System.out.println(list);
		// [Student [id=1, name=张三, age=28], Student [id=2, name=李四, age=16],
		// Student [id=2, name=李四, age=18], Student [id=3, name=王五, age=22]]
	}

	// 动态参数，相当于数组
	public static void show(Integer... p) {
		for (Integer i : p) {
			System.out.print(i + "\t");
		}
	}
}
